// 第一题
// 节流

// 核心思想:
// 1、在一定的时间范围内执行一次函数
// 2、部分逻辑和防抖一致
let scrollEvevtCount = 0;

let scrollFn = function() {
    scrollEvevtCount++;
    console.log('scroll事件触发:', scrollEvevtCount)
};

let throttle = function(fn, delay) {
    let timer = null;
    let pre = new Date().getTime();
    return function(...ags) {
        let now = new Date().getTime();
        if (now - pre > delay) {
            fn.apply(this, ags);
            pre = now;
        } else {
            if (timer) {
                clearTimeout(timer);
                timer = null;
            };
            timer = setTimeout(function () {
                fn.apply(this, ags);
                pre = now;
             }, delay)
        }
    }
}


document.body.onscroll = throttle(scrollFn, 500);




